import { login, logout, getInfo, passport } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  roles: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  }
}

const actions = {


  // user passport
  passport({ commit }, passportstr) {
    const passport = passportstr
    return new Promise((resolve, reject) => {
      //TODO 后台接口实现后再添加
      // passport({ username: passport.trim() }).then(response => {

        let Base64 = require('js-base64').Base64
        let up = JSON.parse(Base64.decode(passportstr))
        login(up).then(response => {
          const data = response.data
          const tokenStr = data.tokenHead + data.token
          setToken(tokenStr)
          commit('SET_TOKEN', tokenStr)
          resolve()
        }).catch(error => {
          reject(error)
        })

        // let response = {"code":200,"message":"操作成功","data":{"tokenHead":"Bearer","token":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJtb25pdG9yIiwiY3JlYXRlZCI6MTYwMzE2MDE4MDU2OSwiZXhwIjoxNjAzNzY0OTgwfQ.0ZmeYLsn5r8D1rd9q-8V1ohVaM_fVhoXLdYLPlIFXvE0KSwe4m-vHynp8qPldvu1WfyZWIHlRzzw7tsJMwE7ag"}}

        // const data = response.data
        // const tokenStr = data.tokenHead + data.token
        // setToken(tokenStr)
        // commit('SET_TOKEN', tokenStr)
        // resolve()
      // }).catch(error => {
      //   reject(error)
      // })
    })
  },

  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const data = response.data
        const tokenStr = data.tokenHead + data.token
        setToken(tokenStr)
        commit('SET_TOKEN', tokenStr)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {

        const data = response.data
        if (data.roles && data.roles.length > 0) { //
          commit('SET_ROLES', data.roles)
        } else {
          reject('getInfo: roles must be a non-null array !')
        }
        commit('SET_NAME', data.username)
        commit('SET_AVATAR', data.icon)
        // console.log(data);

        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // logout(state.token).then(() => {
      commit('SET_TOKEN', '')
      removeToken()
      resetRouter()
      resolve()
      // }).catch(error => {
      //   reject(error)
      // })
    })
  },

  // remove token
  FedLogOut({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

